/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package acousticfield3d.gui.misc;

import acousticfield3d.algorithms.CalcField;
import acousticfield3d.gui.MainForm;
import acousticfield3d.math.M;
import acousticfield3d.math.Vector3f;
import acousticfield3d.scene.Entity;
import acousticfield3d.utils.Parse;
import acousticfield3d.utils.TextFrame;

/**
 *
 * @author am14010
 */
public class ForcePlotsFrame extends javax.swing.JFrame {
    final MainForm mf;

    public ForcePlotsFrame(MainForm mf) {
        this.mf = mf;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        nPointsText = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        dispText = new javax.swing.JTextField();
        showButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("NPoints:");

        nPointsText.setText("25");

        jLabel2.setText("Disp:");

        dispText.setText("0.05");

        showButton.setText("Show");
        showButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nPointsText, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dispText, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(showButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nPointsText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(dispText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(showButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void showButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showButtonActionPerformed
        final int nSteps = Parse.toInt( nPointsText.getText() );
        final float disp = Parse.toFloat( dispText.getText() );
        
        Entity bead = mf.scene.getFirstWithTag( Entity.TAG_CONTROL_POINT | Entity.TAG_BEAD);
        if (bead == null){
            return;
        }
        final Vector3f pos = bead.getTransform().getTranslation();
        
        final float particleR = bead.getTransform().getScale().maxComponent() / 2.0f;

        StringBuilder sb = new StringBuilder();
        sb.append("X \t Y \t Z \t ForceX \t ForceY \t ForceZ \t pressureX \t pressureY \t pressureZ \t gorkX \t gorkY \t gorkZ\n");
        for(int i = 0; i < nSteps; ++i){
            final float dispS = ( (i / (float)nSteps) - 0.5f) * disp;
           
            final Vector3f forceX = CalcField.calcForceAt(pos.x + dispS, pos.y, pos.z, particleR, mf);
            final Vector3f forceY = CalcField.calcForceAt(pos.x, pos.y + dispS, pos.z, particleR, mf);
            final Vector3f forceZ = CalcField.calcForceAt(pos.x, pos.y, pos.z + dispS, particleR, mf);
            final double pressureX = CalcField.calcFieldAt(pos.x + dispS, pos.y, pos.z, mf).length();
            final double pressureY = CalcField.calcFieldAt(pos.x, pos.y + dispS, pos.z, mf).length();
            final double pressureZ = CalcField.calcFieldAt(pos.x, pos.y, pos.z + dispS, mf).length();
            final double gorkX = CalcField.calcGorkovAt(pos.x + dispS, pos.y, pos.z, particleR,mf);
            final double gorkY = CalcField.calcGorkovAt(pos.x, pos.y + dispS, pos.z, particleR,mf);
            final double gorkZ = CalcField.calcGorkovAt(pos.x, pos.y, pos.z + dispS, particleR,mf);
            
            
            sb.append( (pos.x + dispS) + " \t");
            sb.append( (pos.y + dispS) + " \t");
            sb.append( (pos.z + dispS) + " \t");
            sb.append(forceX.x + " \t");
            sb.append(forceY.y + " \t");
            sb.append(forceZ.z + "\t");
            sb.append(pressureX + "\t");
            sb.append(pressureY + "\t");
            sb.append(pressureZ + "\t");
            sb.append(gorkX + "\t");
            sb.append(gorkY + "\t");
            sb.append(gorkZ + "\n");
        }
        
        TextFrame.showText("Forces", sb.toString(), this);
    }//GEN-LAST:event_showButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField dispText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField nPointsText;
    private javax.swing.JButton showButton;
    // End of variables declaration//GEN-END:variables
}
